package com.choudou5.solr.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;


public class SysSeqUtil {

    private static final Logger logger = LoggerFactory.getLogger(SysSeqUtil.class);

    private static Snowflake snowflake = new Snowflake(1);

    private static AES aes = SecureUtil.aes();

    /**
     * 生成ID
     * @return 19位长度数字
     */
    public static Long generateId() {
        return snowflake.nextId();
    }

    public static String[] CHARS = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };

    public static String generateCode() {
        StringBuffer uuid = new StringBuffer(32);
        char[] chars = UUID.randomUUID().toString().toCharArray();
        for (char c : chars) {
            if(c != '-')
                uuid.append(c);
        }
        StringBuffer uuidShort = new StringBuffer(8);
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            uuidShort.append(CHARS[x % 0x3E]);
        }
        return uuidShort.toString().toUpperCase()+ RandomUtil.randomString(5).toUpperCase();
    }

    public static String genTimeIdStr() {
        String id = DateUtil.format(new Date(), "yyMMddHHmmssSSS");
        return id;
    }
    public static Long genTimeId() {
        String id = DateUtil.format(new Date(), "yyMMddHHmmssSSS");
        return Long.valueOf(id);
    }
    public static Long genTimeId(int add) {
        String id = DateUtil.format(new Date(), "yyMMddHHmmssSSS");
        return Long.valueOf(id)+add;
    }


    /**
     * 加密ID
     * @param id
     * @return
     */
    public static String encryptId(String id) {
        return aes.encryptHex(id);
    }

    /**
     * 解密ID
     * @param code
     * @return
     */
    public static String decryptId(String code) {
        return aes.decryptStr(code);
    }

    public static Integer decryptIntId(String code) {
        try {
            return Integer.parseInt(aes.decryptStr(code));
        } catch (Exception e) {
            logger.error(code+" 解密ID失败", e);
        }
        return null;
    }


    public static void main(String[] args) {
/*        Set<String> set = null;
        for (int j = 0; j < 1000; j++) {
            set = new HashSet<>();
            for(int i = 0; i < 2000; ++i) {
//                System.out.println(UUIDUtil.uuid());
//                System.out.println(snowflake.nextId());
//                System.out.println(generateCode());
                set.add(generateCode());
            }
            if(200000 != set.size())
                System.out.println(j+"-----"+set.size());
        }*/

        String id = "1232342344";
        String code = decryptId(id);
        System.out.println(code);
        System.out.println(decryptIntId("ae1c6d54553f30be66d5bfe750ef6f02"));
    }

}
